package com.xingu.xg.controller;


import com.xingu.xg.domain.dto.TreeNodeDto;
import com.xingu.xg.domain.request.AddNodeQuery;
import com.xingu.xg.domain.request.CopyNodeQuery;
import com.xingu.xg.domain.request.DeleteNodeQuery;
import com.xingu.xg.service.TreeNodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@Api(tags = "树形结构管理")
@RequestMapping("/tree")
public class TreeNodeController {

    private final TreeNodeService treeNodeService;

    public TreeNodeController(TreeNodeService treeNodeService) {
        this.treeNodeService = treeNodeService;
    }

    @ApiOperation("查询树形结构列表")
    @GetMapping("/list")
    public List<TreeNodeDto> queryTreeList() {
        // 假设根节点parentId为"0"或null，根据实际情况调整
        return treeNodeService.queryTreeList();
    }

    @ApiOperation("创建子级节点")
    @PostMapping("/create/child")
    public List<TreeNodeDto> createChildNode(@Valid @RequestBody AddNodeQuery request) {
        return treeNodeService.addNode(request);
    }

    @ApiOperation("复制粘贴节点（保持层级）")
    @PostMapping("/copy")
    public List<TreeNodeDto> copyNodes(@Valid @RequestBody CopyNodeQuery query) {
        return treeNodeService.copyNodes(query);
    }

    @ApiOperation("批量删除节点")
    @PostMapping("/delete")
    public List<TreeNodeDto> deleteNodes(@Valid @RequestBody DeleteNodeQuery query) {
        return treeNodeService.deleteNodes(query);
    }
}